import $util from "@/utils/index.js"
import $api from "@/api/index.js"
import siteInfo from '@/siteinfo.js'
export default {
	state: {
		// #ifdef APP-PLUS
		recommend_text: '热门',
		// #endif
		// #ifndef APP-PLUS
		recommend_text: '推荐',
		// #endif
		old_attendant_name: '工作者',
		old_reseller_menu_name: '分销员',
		old_broker_menu_name: '经纪人',
		old_channel_menu_name: '渠道商',
		old_agent_default_name: '代理商',
		old_life_text: '生活照',
		old_material_text: '物料费',

		// 当前服务列表/工作者列表/订单列表/个人中心样式最大值，方便兼容新增样式后，小程序以及App显示
		service_list_type: 4,
		coach_list_format: 4,
		order_list_type: 2,
		mine_page_type: 3,
		base_img_url: siteInfo.siteroot.split('/index.php')[0] + '/h5/static/img',
		coachTabBar: [{
			id: 1,
			name: '首页',
			default_img: 'icontab-shouye2',
			selected_img: 'icontab-shouye2'
		}, {
			id: 2,
			name: '订单',
			default_img: 'icontab-dingdan2',
			selected_img: 'icontab-dingdan2'
		}, {
			id: 3,
			name: '统计',
			default_img: 'icontab-tongji2',
			selected_img: 'icontab-tongji2'
		}, {
			id: 4,
			name: '我的',
			default_img: 'icontab-wode2',
			selected_img: 'icontab-wode2'
		}],
		configInfo: {
			id: 0,
			isIos: uni.getSystemInfoSync().system.includes('iOS'),
			navBarHeight: uni.getSystemInfoSync().statusBarHeight * 1 + 44,
			curSysHeight: '',
			tabbarHeight: '',
			tabBar: [],
		},
		pageArr: ['/pages/service', '/pages/technician', '/pages/shopstore', '/pages/partner', '/pages/dynamic',
			'/pages/map',
			'/pages/order', '/pages/mine'
		],
		play_method: [{
			method: 'onPlay',
			msg: '开始播放',
			status: true,
		}, {
			method: 'onStop',
			msg: '结束播放',
			status: false,
		}, {
			method: 'onError',
			msg: '报错Error',
			status: false,
		}, {
			method: 'onEnded',
			msg: '自然结束播放',
			status: false,
		}],
		audioBg: {},
		audioType: 'service_start_recording',
		playBg: false,
		isHaveAudio: false,
		sysheight: 0
	},
	mutations: {
		//修改信息
		updateConfigItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
			if (!['configInfo', 'audioType'].includes(key)) return
			let src = state.configInfo[state.audioType]
			if (key == 'configInfo') {
				uni.setStorageSync('configInfo', val)
			}
			if (key == 'audioType') {
				// if (!src) return
				if (state.playBg) {
					state.audioBg.stop()
				}
			}
			if (state.isHaveAudio) {
				state.audioBg.src = src || ''
				if (key == 'configInfo') return
			} else {
				if (!src) return
				state.audioBg = uni.createInnerAudioContext();
				state.isHaveAudio = true
				state.audioBg.src = src
				// #ifndef APP-PLUS
				state.audioBg.obeyMuteSwitch = false
				// #endif
				state.play_method.map(item => {
					state.audioBg[item.method](() => {
						console.log('bg=>', item.msg)
						state.playBg = item.status;
					})
				})
			}
			if (key == 'audioType') {
				state.audioBg.play()
			}
		}
	},
	actions: {
		// 获取基本配置
		async getConfigInfo({
			commit,
			state
		}, param) {

			let rules_status = 0
			if (param && param.rules_status) {
				rules_status = param.rules_status
			}
			try {

				let config = await $api.base.configInfo({
					rules_status: rules_status == 0 ? 1 : 0
				})

				config.hide_order_info = 0
				config.plugAuth.hotel = 0 // 酒店
				config.plugAuth.career = 0 // 职业类型
				config.app_model_type = 0 // app类型 0其他；1华为；
				config.is_app_user = 0 // 是否是app用户端 0不是；1是

				if (!rules_status && !config.hasOwnProperty('app_wechat_pay')) {
					config.app_wechat_pay = 1 // app审核是否有微信支付功能
				}

				// #ifndef APP-PLUS
				config.app_wechat_pay = 1
				if (config.is_app_user) {
					config.plugAuth.coachport = true
				}
				// #endif

				// #ifdef MP-WEIXIN-USER || H5-USER
				config.plugAuth.coachport = true
				// #endif

				if (!config.pageColor) {
					config.pageColor = '#f6f6f6'
				}


				config.tabBar = config.tabBar.filter(item => {
					return item.id != 7
				})

				// 将“操作端”入口添加到底部 TabBar（若未存在），并放在“我的”前面
				const operateTab = { id: 9, name: '工作者端', default_img: 'iconziliao', selected_img: 'iconziliao' }
				const hasOperate = config.tabBar.findIndex(item => { return item.id === operateTab.id || item.name === '工作者端' || item.name === '操作端' }) !== -1
				if (!hasOperate) {
					// 优先找到“我的”所在位置（通常 id 为 5），插入其前面
					let myIndex = config.tabBar.findIndex(item => { return item.id === 5 || item.name === '我的' })
					if (myIndex !== -1) {
						config.tabBar.splice(myIndex, 0, operateTab)
					} else {
						config.tabBar.push(operateTab)
					}
				}

				let service_list_type = 1
				let order_list_type = 1
				let mine_page_type = 1
				let isInit = config.page[5].length === 0
				if (isInit) {
					let bannerInd = config.page[1].findIndex(item => {
						return ['banner', 'service-banner'].includes(item.type)
					})
					let columnInd = config.page[1].findIndex(item => {
						return ['column', 'service-column'].includes(item.type)
					})
					let recommendInd = config.page[1].findIndex(item => {
						return item.type === 'coach-recommend'
					})
					let serviceInd = config.page[1].findIndex(item => {
						return item.type === 'service-list'
					})

					if (bannerInd === -1) {
						config.page[1].unshift({
							type: 'service-banner',
							data: {
								isShowFilter: true
							}
						})
					}
					if (columnInd === -1) {
						config.page[1].push({
							type: 'service-column',
							data: {}
						})
					}
					if (recommendInd === -1) {
						config.page[1].push({
							type: 'coach-recommend',
							data: {
								title: `${state.recommend_text}${config.attendant_name}`,
								type: 1
							}
						})
					}
					if (serviceInd == -1) {
						config.page[1].push({
							type: 'service-list',
							data: {
								title: `服务列表`,
								type: 1
							}
						})
					}
				}

				config.is_industry_cate = 0
				config.page[1].map(item => {
					if (item.type === 'service-list') {
						if (item.data.type > state.service_list_type) {
							item.data.type = 1
						}
						service_list_type = item.data.type
					}
					if (item.type === 'industry-cate-list') {
						config.is_industry_cate = 1
					}
				})
				if (config.page[4] && config.page[4].length > 0) {
					if (config.page[4][0].data.type > state.order_list_type) {
						config.page[4][0].data.type = 1
					}
					order_list_type = config.page[4][0].data.type
				}
				if (config.page[5] && config.page[5].length > 0) {
					if (config.page[5][0].data.type > state.mine_page_type) {
						config.page[5][0].data.type = 1
					}
					mine_page_type = config.page[5][0].data.type
				}


				let base_config = $util.pick(state.configInfo, ['curSysHeight',
					'tabbarHeight',
				])

				let {
					system,
					statusBarHeight,
					navigationBarHeight
				} = await uni.getSystemInfoSync()
				let isIos = system.includes('iOS')
				let navBarHeight = statusBarHeight * 1 + 44

				let data = Object.assign({}, config, base_config, {
					isIos,
					navBarHeight,
					service_list_type,
					order_list_type,
					mine_page_type,
					coachTabBar: state.coachTabBar,
				})
				if (data.coach_list_format > state.coach_list_format) {
					data.coach_list_format = 1
				}
				commit('updateConfigItem', {
					key: 'configInfo',
					val: data
				})
			} catch (e) {
				let data = Object.assign({}, state.config, {
					primaryColor: '#FF4564',
					subColor: '#F1C06B',
					pageColor: '#f4f6f8',
				})
				commit('updateConfigItem', {
					key: 'configInfo',
					val: data
				})
			}
		},
		toPlayAudio({
			commit,
			state
		}, param) {
			let {
				key
			} = param
			console.log("toPlayAudio========", key)
			commit('updateConfigItem', {
				key: 'audioType',
				val: key
			})
		}
	}
}